const mongoose = require('mongoose');
var rp = require('request-promise');
//分页
const getMoviesData = async ({ curPage, eachPage }) => {
    // await mongoose.model('students').find();
    const result = {
        curPage: ~~curPage,
        eachPage: ~~eachPage,
        total: 0,
        maxPage: 0,
        rows: [],
    }
    const movesModel = mongoose.model('movies');
    //获取总条数
    result.total = await movesModel.countDocuments();
    result.maxPage = Math.ceil(result.total / result.eachPage);
    result.rows = await movesModel.find()
        .skip((result.curPage - 1) * result.eachPage)
        .limit(result.eachPage)
        .sort({
            _id: -1
        })
        .exec();

    return result
}
//更新
const update = async (moviesMsg) =>
    await mongoose.model('movies')
        .findByIdAndUpdate(moviesMsg._id, {
            cname: moviesMsg.cname,
            ename: moviesMsg.ename,
            type: moviesMsg.type,
            area: moviesMsg.area,
            poster: moviesMsg.poster,
            time: moviesMsg.time,
            upDate: moviesMsg.upDate,
            score: moviesMsg.score,
            count: moviesMsg.count,
            intro: moviesMsg.intro,
            isClassic: moviesMsg.isClassic,
            director: moviesMsg.director,
            actor: moviesMsg.actor,
        }, (err, ret) => {
            data = err ? console.log("失败", err) : console.log('成功');
        });
//删除电影
const deleteMovies = async ({ _id } = {}) =>
    await mongoose.model('movies').remove({ _id }, (err, ret) => {
        if (err) {
            console.log("删除失败");
        } else {
            console.log("删除成功");
        }
    })

//新增电影
const addMovies = async (data) => {
    const flag = await mongoose.model('movies').create(data)
    return flag
}

//查询
const serchMovies = async (data) => {
    const { cname = "", type = "", director = "", actor = "", area = "", upDate = "", curpage , eachpage  } = data;
    const result = {
        curpage: ~~curpage,
        eachpage: ~~eachpage,
        total: 0,
        maxpage: 0,
        rows: [],
    }
   

    result.rows = await mongoose.model('movies').find(
        {
            cname: {
                $regex: String(cname)
            },
            type: {
                $regex: String(type)
            },
            director: {
                $regex: String(director)
            },
            actor: {
                $regex: String(actor)
            },
            area: {
                $regex: String(area)
            },
            upDate: {
                $regex: String(upDate)
            }
        }
    )
    result.total = result.rows.length;

    result.rows = await mongoose.model('movies').find(
        {
            cname: {
                $regex: String(cname)
            },
            type: {
                $regex: String(type)
            },
            director: {
                $regex: String(director)
            },
            actor: {
                $regex: String(actor)
            },
            area: {
                $regex: String(area)
            },
            upDate: {
                $regex: String(upDate)
            }
        }
    ).skip((result.curpage - 1) * result.eachpage)
    .limit(result.eachpage)
    .exec()
    result.maxpage = Math.ceil(~~result.total / result.eachpage);
    return result
}

const getOne = async _id =>
    await mongoose.model('movies').findOne({ _id });
//添加海报图片
const addPostImgs = async ({ _id, url }) =>
    await mongoose.model('movies').updateOne({ _id }, {
        $push: {
            //图片存放哪个数据库数组
            posterImg: url
        }
    });

//添加剧集图片
const addSetImgs = async ({ _id, url }) =>
    await mongoose.model('movies').updateOne({ _id }, {
        $push: {
            //图片存放哪个数据库数组
            setImg: url
        }
    });
//添加相关人物图片
const addHeadImgs = async ({ _id, url }) =>
    await mongoose.model('movies').updateOne({ _id }, {
        $push: {
            //图片存放哪个数据库数组
            headImg: url
        }
    });
//删除图片
const deleteImgs = async ({ _id, url }, types) => {
    const data = await getOne({ _id });
    switch (types) {
        case "PostImg":
            data.posterImg.pull(url)
            break;
        case "HeadImg":
            data.headImg.pull(url)
            break;
        case "SetsImg":
            data.setImg.pull(url)
            break;
    }

    var options = {
        method: 'delete',
        uri: `http://localhost:3002/movies/${_id}/imgs/${encodeURIComponent(url)}`,
        // headers: {
        //     "authorization": token
        // },
        json: true // Automatically stringifies the body to JSON
    };

    await rp(options)
    return await data.save();
}
//修改电影  
const updateMovies = async ({ _id }) => {
    const data = await getOne({ _id });
    return data
}


module.exports = { getMoviesData, update, deleteMovies, addMovies, serchMovies, getOne, addPostImgs, addSetImgs, addHeadImgs, deleteImgs, updateMovies }